package com.falm.picturebackend.entity.convert;

import cn.hutool.core.collection.CollectionUtil;
import com.falm.picturebackend.entity.BO.FilesBO;
import com.falm.picturebackend.entity.DO.FilesDO;
import com.falm.picturebackend.entity.VO.FilesVO;
import com.falm.picturebackend.util.ApplicationContextUtil;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
public class FilesConvert {

    public static FilesBO do2BO(FilesDO filesDO) {
        if (filesDO == null) {
            return null;
        }
        FilesBO filesBO = new FilesBO();
        filesBO.setId(filesDO.getId());
        filesBO.setUuid(filesDO.getUuid());
        filesBO.setName(filesDO.getName());
        filesBO.setIsFolder(filesDO.getIsFolder());
        filesBO.setParentUuid(filesDO.getParentUuid());
        filesBO.setFileSize(filesDO.getFileSize());
        filesBO.setFileExtension(filesDO.getFileExtension());
        filesBO.setMimeType(filesDO.getMimeType());
        filesBO.setStoragePath(filesDO.getStoragePath());
        filesBO.setStorageType(filesDO.getStorageType());
        filesBO.setStatus(filesDO.getStatus());
        filesBO.setLevel(filesDO.getLevel());
        filesBO.setFullPath(filesDO.getFullPath());
        filesBO.setCreateTime(filesDO.getCreateTime());
        filesBO.setUpdateTime(filesDO.getUpdateTime());
        return filesBO;
    }

    public static List<FilesBO> do2Forest(List<FilesDO> filesDOList) {
        List<FilesBO> filesBOList = do2BOList(filesDOList);
        Map<String, List<FilesBO>> filesBoHashMap =
            filesBOList.stream().collect(Collectors.groupingBy(FilesBO::getParentUuid));

        List<FilesBO> filesBoS = filesBoHashMap.get(" ");
        if (filesBoS == null) {
            return new ArrayList<>();
        }
        filesBoS.forEach(filesBO -> doForest(filesBO, filesBoHashMap));
        return filesBoS;
    }

    private static void doForest(FilesBO parentFile, Map<String, List<FilesBO>> filesBoHashMap) {
        List<FilesBO> childrenFileList = filesBoHashMap.get(parentFile.getUuid());
        if (CollectionUtil.isEmpty(childrenFileList)) {
            return;
        }
        parentFile.setChildren(childrenFileList);
        childrenFileList.forEach(childFile -> {
            childFile.setParent(parentFile);
            doForest(childFile, filesBoHashMap);
        });
    }

    public static FilesDO bo2DO(FilesBO filesBO) {
        if (filesBO == null) {
            return null;
        }
        FilesDO filesDO = new FilesDO();
        filesDO.setId(filesBO.getId());
        filesDO.setUuid(filesBO.getUuid());
        filesDO.setName(filesBO.getName());
        filesDO.setIsFolder(filesBO.getIsFolder());
        filesDO.setParentUuid(filesBO.getParentUuid());
        filesDO.setFileSize(filesBO.getFileSize());
        filesDO.setFileExtension(filesBO.getFileExtension());
        filesDO.setMimeType(filesBO.getMimeType());
        filesDO.setStoragePath(filesBO.getStoragePath());
        filesDO.setStorageType(filesBO.getStorageType());
        filesDO.setStatus(filesBO.getStatus());
        filesDO.setLevel(filesBO.getLevel());
        filesDO.setFullPath(filesBO.getFullPath());
        filesDO.setCreateTime(filesBO.getCreateTime());
        filesDO.setUpdateTime(filesBO.getUpdateTime());
        return filesDO;
    }

    public static FilesVO bo2VO(FilesBO filesBO) {
        if (filesBO == null) {
            return null;
        }
        FilesVO filesVO = new FilesVO();
        filesVO.setId(filesBO.getId());
        filesVO.setUuid(filesBO.getUuid());
        filesVO.setName(filesBO.getName());
        filesVO.setIsFolder(filesBO.getIsFolder());
        filesVO.setParentUuid(filesBO.getParentUuid());
        filesVO.setFileSize(filesBO.getFileSize());
        filesVO.setFileExtension(filesBO.getFileExtension());
        filesVO.setMimeType(filesBO.getMimeType());
        filesVO.setStoragePath(filesBO.getStoragePath());
        filesVO.setStorageType(filesBO.getStorageType());
        filesVO.setStatus(filesBO.getStatus());
        filesVO.setLevel(filesBO.getLevel());
        filesVO.setFullPath(filesBO.getFullPath());
        filesVO.setCreateTime(filesBO.getCreateTime());
        filesVO.setUpdateTime(filesBO.getUpdateTime());
        return filesVO;
    }

    public static List<FilesBO> do2BOList(List<FilesDO> filesDOList) {
        return filesDOList.stream().map(FilesConvert::do2BO).collect(Collectors.toList());
    }

    public static List<FilesDO> bo2DOList(List<FilesBO> filesBOList) {
        return filesBOList.stream().map(FilesConvert::bo2DO).collect(Collectors.toList());
    }
}